Ext.define('SRMT.controller.project-explorer.NewProject', {
	extend: 'Ext.app.Controller',
	
	views: [
	        'project-explorer.NewProject',
	        'Viewport'
	],
	
	refs: [{
		ref: 'newProjectForm',
		selector: 'srmtNewProject'
	},{
		ref:'edtProjectName',
		selector: 'srmtNewProject > #edtProjectName'
	},{
		ref: 'btnReset',
		selector: 'srmtNewProject #btnReset'
	},{
		ref: 'btnSubmit',
		selector: 'srmtNewProject #btnSubmit'
	},{
		ref: 'viewport',
		selector: 'viewport'
	}],
	
	init: function() {
		this.control({
			'srmtNewProject': {
				render: this.onRender
			},
			
			'srmtNewProject > #edtProjectName': {
			},
			
			'srmtNewProject #btnReset': {
				click: this.resetForm
			},
			
			'srmtNewProject #btnSubmit': {
				click: this.submitForm
			}
		});
	},
	
	onRender: function() {
		
	},
	
	resetForm: function() {
		this.getNewProjectForm().getForm().reset();
	},
	
	submitForm: function() {
		var form = this.getNewProjectForm().getForm();
		var winNewProjectForm = this.getNewProjectForm().up('window');
		var projectExplorerStore = this.getStore('SRMT.store.ProjectExplorer');
		if(form.isValid()) {
//			console.log('project_name'+project_name);
			form.submit({
				url: 'php/module/project_explorer/add_new_project.php',
				method: 'POST',
//				params: {
//					projectname: projectname
//				},
				async: true,
				waitMsg: 'Creating new project ...',
				timeout: 5000,
				success: function(form, action) {
					Ext.MessageBox.show({
						title: 'New Project',
						msg: 'Create successfully.',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.INFO,
						fn: function(btn) {
							if(btn == 'ok') {
								projectExplorerStore.load();
							}
						}
						});
					winNewProjectForm.destroy();
                },
				failure: function(form, action) {
					console.log(action.result);
					Ext.MessageBox.show({
						title: 'New Project',
						msg: 'Create fail!'+action.result.pro,
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.ERROR
						});
				}
			});
		}
		
	}
});